﻿// JSON C# Class Generator
// http://at-my-window.blogspot.com/?page=json-class-generator

using System;
using Newtonsoft.Json.Linq;
using JsonCSharpClassGenerator;
using TomatoSynchronizer.Store;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace TomatoSynchronizer.Entities
{

	public class UnitClass
	{
		public override bool Equals(object obj)
		{
			if(obj is UnitClass)
			{
				UnitClass uclass = (UnitClass)obj;
				
				if(uclass.is_default == _is_default)
				{
					MappingStore mappingStore = new MappingStore(uclass.store.site,store.site,"languages_id");
					
					foreach(Mapping mapping in mappingStore.Mappings)
					{
						try
						{														
							string titleDest = uclass.unit_class_titles["unit_class_title[" + mapping.DestValue + "]"];
							string titleSrc = unit_class_titles["unit_class_title[" + mapping.SrcValue + "]"];
							
							if(titleSrc != titleDest)
							{
								return false;
							}
						}
						catch(KeyNotFoundException)
						{
							return false;
						}
					}
					return true;
				}
			}
			
			return false;
		}
		
		public UnitClass()
		{
		}

		public UnitClass(string json)
			: this(JObject.Parse(json))
		{
		}

		private JObject __jobject;
		public UnitClass(JObject obj)
		{
			this.__jobject = obj;
		}

		private UnitClassStore store;

		public UnitClassStore Store
		{
			get{
				return store;}
			set{
				store = value;}
		}
		
		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private string _unit_class_id;
		public string unit_class_id
		{
			get
			{
				if(_unit_class_id == null)
					_unit_class_id = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "unit_class_id"));
				return _unit_class_id;
			}
			set
			{
				_unit_class_id = value;
			}
		}
		
		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private string _language_id;
		public string language_id
		{
			get
			{
				if(_language_id == null)
					_language_id = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "language_id"));
				return _language_id;
			}
			set
			{
				_language_id = value;
			}
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private int _is_default;
		public int is_default
		{
			get
			{
				if(_is_default == 0 && __jobject != null)
				{
					try {
						_is_default = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(__jobject, "is_default"));
					} catch (Newtonsoft.Json.JsonSerializationException) {
						
					}
				}					
				return _is_default;
			}
			set
			{
				_is_default = value;
			}
		}
		
		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private Dictionary<string, string> _unit_class_titles;
		public Dictionary<string, string> unit_class_titles
		{
			get
			{
				if(_unit_class_titles == null)
				{
					_unit_class_titles = new Dictionary<string, string>();
					
					if(__jobject != null)
					{
						if(store != null)
						{
							var languages = store.site.GetLanguageStore().LoadItems();
							
							foreach(Language language in languages)
							{
								_unit_class_titles["unit_class_title[" + language.languages_id + "]"] = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject,"unit_class_title[" + language.languages_id + "]"));
							}
						}
					}
				}
				
				return _unit_class_titles;
			}
			set
			{
				_unit_class_titles = value;
			}
		}
	}
}
